#include"ProxyNum.h"
#include"NumArray.h"

ProxyNum::ProxyNum() : object_(0) {}

ProxyNum::ProxyNum(NumArray * object, size_t index) : object_(object), index_(index) {}

ProxyNum & ProxyNum::operator =(unsigned long long const & value) {
    if(object_) {
        if(index_ < object_->size()) {
            if(object_->size_ == 0) {
                if(value < MAX_SMALL_NUM) {
                    object_->number_ = 0;
                    object_->number_ += value;
                } else {
                    object_->convertToLargeNum(value);
                }
            } else {
                object_->number_[index_] = value;
            }
        }
    }
    return *this;
}

ProxyNum & ProxyNum::operator +=(ProxyNum const & right) {
    (*this) = (int)object_->number_[index_] + (int)right.object_->number_[right.index_];
    return *this;
}

ProxyNum & ProxyNum::operator -=(ProxyNum const & right) {
    (*this) = (int)object_->number_[index_] - (int)right.object_->number_[right.index_];
    return *this;
}

ProxyNum & ProxyNum::operator *=(ProxyNum const & right) {
    (*this) = (int)object_->number_[index_] * (int)right.object_->number_[right.index_];
    return *this;
}

ProxyNum::operator int() const{
    if(index_ < object_->size()) {
        if(object_->isSmall()) {
            return int(object_->number_);
        }
        return object_->number_[index_];
    }
    return -1;
}
